home *** CD-ROM | disk | FTP | other *** search
Text File | 1987-06-27 | 3.4 KB | 130 lines | [TEXT/MACA] |
- PROGRAM Big_Array;
- {
- A demonstration of large arrays. Three arrays are dimensioned,
- each about 100K. Turbo Pascal v1.00A
- }
- USES MemTypes,QuickDraw,OSIntf;
-
- CONST
- intLenght = 2; { For integer arrays. }
-
- TYPE
- big = record
- base : Ptr;
- i, j : longint
- end;
- IntPtr = ^integer;
-
- VAR
- A, B, Sum : big;
- i, j, seconds : LONGINT;
- thePtr : IntPtr;
- theRow, theCol,
- theLines : INTEGER;
-
- PROCEDURE cleanUp; FORWARD;
-
- PROCEDURE dimension( VAR theArray : big; k,l : LONGINT);
-
- BEGIN { dimension }
- WITH theArray DO
- BEGIN
- base := NewPtr(k*l*intLenght);
- i := k;
- j := l
- END { WITH theArray }
- END; { of dimension }
-
- FUNCTION Index(VAR theArray : big; k,l : LONGINT):Ptr;
- {
- Kind of like N dimensional arrays in FORTRAN
- }
- BEGIN { Index }
- WITH theArray DO
- BEGIN
- IF (k > i) or (l > j) then { Do a range test. }
- BEGIN
- WriteLn('Range Error');
- ReadLn;
- cleanUp;
- halt
- END { IF }
- ELSE
- Index := Pointer(i * (k - 1)*intLenght + j * (l - 1)*intLenght
- + LONGINT(base))
- END { WITH }
- END; { of Index }
-
- PROCEDURE cleanUp;
-
- BEGIN
- disposPtr(A.base);
- disposPtr(B.base);
- disposPtr(Sum.base);
- END;
-
- PROCEDURE Plus( VAR Array1, Array2, SumArray: big);
-
- VAR
- RowIndex, ColIndex : LONGINT;
- Sum : INTEGER;
-
- BEGIN { Plus }
- IF (Array1.i <> Array2.i) or (Array2.i <> SumArray.i)
- or (Array1.j <> Array2.j) or (Array2.j <> SumArray.j) then
- BEGIN
- WriteLn( 'Arrays are not similar.');
- ReadLn;
- Halt
- END { IF }
- ELSE
- FOR RowIndex := 1 to SumArray.i DO
- FOR ColIndex := 1 to SumArray.j DO
- BEGIN
- thePtr := IntPtr(Index(Array1,RowIndex,ColIndex));
- Sum := thePtr^;
- thePtr := IntPtr(Index(Array2,RowIndex,ColIndex));
- Sum := Sum + thePtr^;
- thePtr := IntPtr(Index(SumArray,RowIndex,ColIndex));
- thePtr^ := Sum
- END { FOR }
- END; { of Plus }
-
- FUNCTION Randomize (range : INTEGER): INTEGER;
- { See Chernicoff V1 p 25 }
-
- VAR
- rawResult : LONGINT;
-
- BEGIN { Randomize }
-
- rawResult := Abs(Random);
- Randomize := (rawResult * range ) div 32768
-
-
- END; { Randomize }
-
- BEGIN { Big_Array }
- WriteLn('Be patient.');
- dimension(A,255,255);
- dimension(B,255,255);
- dimension(Sum,255,255);
- FOR i := 1 to 255 DO { Initialize the arrays. }
- FOR j := 1 to 255 DO
- BEGIN
- thePtr := IntPtr(index(A,i,j));
- thePtr^ := i + j;
- thePtr := IntPtr(index(B,i,j));
- thePtr^ := 2
- END;
- Plus(A,B,Sum);
- FOR theLines := 1 to 20 DO { Just write some random samples as a test. }
- BEGIN
- theRow := Randomize(255);
- theCol := Randomize(255);
- thePtr := IntPtr(Index(Sum,theRow,theCol));
- WriteLn('The sum at (',theRow,',',theCol,') is ',thePtr^)
- END; { FOR theLines }
- cleanUp;
- ReadLn
- END. { of Big_Array }